package vmcs.entity;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//
import vmcs.utility.VMCSException;

public abstract class Store {

    protected int size;
    protected StoreItem items[];

    public Store() {
    }

    public Store(int itemn) {
        size = itemn;
        items = new StoreItem[size];
    }

    public void setStoreSize(int sz) {
        size = sz;
        items = new StoreItem[size];
    }

    public StoreItem[] getItems() {
        return items;
    }

    public void addItem(int idx, StoreItem object)
            throws VMCSException {
        if (idx >= size) {
            throw new VMCSException("Store.getStoreItem", "Index overflow");
        }
        items[idx] = object;
        if(idx>0){
            items[idx-1].setNext(object);
        }
    }

    public StoreItem getStoreItem(int idx)
            throws VMCSException {
        if (idx >= size) {
            throw new VMCSException("Store.getStoreItem", "Index overflow");
        }
        return items[idx];
    }

    public StoreObject findObject(String name) {
        String en;

        StoreObject so;
        int i;

        for (i = 0; i < size; i++) {
            if (items[i] == null) {
                return null;
            }
            so = items[i].getContent();
            if (so == null) {
                return null;
            }
            en = so.getName();
            if (en != null) {
                if (en.compareTo(name) == 0) {
                    return so;
                }
            }
        }
        return null;
    }

    public void setQuantity(int item, int qty)
            throws VMCSException {
        System.out.println("Stoer.setQauntity: qty:" + qty);
        if (item >= size) {
            throw new VMCSException("Store.setQuantity", "Index overflow");
        }
        items[item].setQuantity(qty);
    }

    public int getStoreSize() {
        return size;
    }
}
